﻿using System;
using System.Threading;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;

namespace l1cs.server.model
{
    public class L1PinkName
    {
        private static Logger _log = Logger.getLogger( typeof( L1PinkName ).FullName );

        private L1PinkName() { }

        class PinkNameTimer
        {
            private L1PcInstance _attacker = null;

            public PinkNameTimer( L1PcInstance attacker )
            {
                _attacker = attacker;
            }

            public void run( Object state )
            {
                for ( int i = 0; i < 180; i++ )
                {
                    Thread.Sleep( 1000 );
                    // 死亡、または、相手を倒して赤ネームになったら終了
                    if ( _attacker.isDead() )
                    {
                        // setPinkName(false);はL1PcInstance#death()で行う
                        break;
                    }
                    if ( _attacker.getLawful() < 0 )
                    {
                        _attacker.setPinkName( false );
                        break;
                    }
                }
                stopPinkName( _attacker );
            }
            private void stopPinkName( L1PcInstance attacker )
            {
                attacker.sendPackets( new S_PinkName( attacker.getId(), 0 ) );
                attacker.broadcastPacket( new S_PinkName( attacker.getId(), 0 ) );
                attacker.setPinkName( false );
            }
        }

        public static void onAction( L1PcInstance pc, L1Character cha )
        {
            if ( pc == null || cha == null )
            {
                return;
            }

            if ( !( cha is L1PcInstance ) )
            {
                return;
            }
            L1PcInstance attacker = (L1PcInstance)cha;
            if ( pc.getId() == attacker.getId() )
            {
                return;
            }
            if ( attacker.getFightId() == pc.getId() )
            {
                return;
            }

            bool isNowWar = false;
            int castleId = L1CastleLocation.getCastleIdByArea( pc );
            if ( castleId != 0 )
            { // 旗内に居る
                isNowWar = WarTimeController.getInstance().isNowWar( castleId );
            }

            if ( pc.getLawful() >= 0
                    && // pc, attacker共に青ネーム
                    !pc.isPinkName() && attacker.getLawful() >= 0
                    && !attacker.isPinkName() )
            {
                if ( pc.getZoneType() == 0 && // 共にノーマルゾーンで、戦争時間内で旗内でない
                        attacker.getZoneType() == 0 && isNowWar == false )
                {
                    attacker.setPinkName( true );
                    attacker.sendPackets( new S_PinkName( attacker.getId(), 180 ) );
                    if ( !attacker.isGmInvis() )
                    {
                        attacker.broadcastPacket( new S_PinkName( attacker.getId(), 180 ) );
                    }
                    PinkNameTimer pink = new PinkNameTimer( attacker );
                    // TODO 優先度9 スレッドプール
                    //GeneralThreadPool.getInstance().execute(pink);
                    //ThreadPool.QueueUserWorkItem(new WaitCallback(pink.run));
                    ThreadPool.QueueUserWorkItem( new WaitCallback( pink.run ) );
                }
            }
        }
    }
}
